<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Jobs\SyncBasicJob;
use Log;
use App\Models\Scm\SyncRecord; 
use Illuminate\Support\Facades\Http;

/**
 * 检查同步调用接口
 */
class SyncCheck extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'collect:syncCheck';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'collect syncCheck';

    protected $baseurl;
    protected $apikey;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->baseurl = "http://" . config('scm.host') . ":" . config('scm.port');
        $this->apikey = config('scm.apikey');
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $scmSyncRecord = new SyncRecord();
        $datalist = $scmSyncRecord->findFailData();
        foreach($datalist as $row){
            $ids = explode(",", $row->ids);
            $url = "";
            if($row->type == "syncAdd"){
                $url = $this->baseurl . '/jeecg-boot/bizInbound/syncAdd';
            }
            else if($row->type == "syncUpdateForAudit"){
                $url = $this->baseurl . '/jeecg-boot/bizInbound/syncUpdateForAudit';
            }
            else if($row->type == "syncUpdateForUnAudit"){
                $url = $this->baseurl . '/jeecg-boot/bizInbound/syncUpdateForUnAudit';
            }
            else if($row->type == "syncUpdateForCancel"){
                $url = $this->baseurl . '/jeecg-boot/bizInbound/syncUpdateForCancel';
            }

            if(!empty($ids)){
                $response = Http::post($url, [
                    'apiKey' => $this->apikey,
                    'idList' => $ids
                ]);
                if ($response->successful()) {
                    $row->status = 'success';
                    $row->save();
                }
            }
        }
        return 0;
    }
}
